<?php
/* creates a compressed zip file */
function create_zip($files = array(), $destination = '', $overwrite = false) {
	//if the zip file already exists and overwrite is false, return false
	if (file_exists($destination) && !$overwrite) {
		return false;
	}
	//vars
	$valid_files = array();
	//if files were passed in...
	if (is_array($files)) {
		//cycle through each file
		foreach ($files as $file) {
			//make sure the file exists
			//echo 'check '.$file;
			if (file_exists($file)) {
				$valid_files[] = $file;
			}
		}
	}
	//if we have good files...
	if (count($valid_files)) {
		//create the archive
		$zip = new ZipArchive();
		if ($zip -> open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
			return false;
		}
		//add the files
		foreach ($valid_files as $file) {
			$zip -> addFile($file, $file);
		}
		//debug
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

		//close the zip -- done!
		$zip -> close();

		//check to make sure the file exists
		return file_exists($destination);
	} else {
		return false;
	}
}

function displayDir($str,&$files=array()) {
	if (!is_dir($str))
		die('不是一个目录！');
	if ($hd = opendir($str)) {
		while ($file = readdir($hd)) {
			if ($file != '.' && $file != '..') {
				if (is_dir($str . '/' . $file)) {
					displayDir($str . '/' . $file,$files);
				} else {
					$files[] =$str.'/'.$file;
				}
			}
		}
	}
	return $files;
}
//echo '<br>';
$files=displayDir('web');
$files[]="DevNote.html";
$files[]="menu.xml";
$file = './tmp/DevNote.zip';
create_zip($files,$file,true);
//down load file.
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}else{
	echo 'file not found';
}
?>